package com.hoshiibuy.idGenerator.consumer.service;

import java.util.Set;
import java.util.TreeSet;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.baidu.fsg.uid.UidGenerator;

@Service
public class UidGenService {
	@Resource
	private UidGenerator uidGenerator;

	private final static Logger logger = LoggerFactory.getLogger(UidGenService.class);

	public long getUid() {
		return uidGenerator.getUID();

	}

	public Set<String> getUidList(int count) {
		if (count <= 0) {
			return null;
		}
		Set<String> uidSet = new TreeSet<String>();
		for (int i = 0; i < count; i++) {
			uidSet.add(String.valueOf(uidGenerator.getUID()));
		}
		logger.debug("要求产生" + count + "个id,实际产生了" + String.valueOf(uidSet.size()) + "个id");
		if (count != uidSet.size()) {
			logger.error("严重错误:产生重复id!!!");
			return null;
		}
		logger.info("id列表:" + uidSet.toString());
		return uidSet;
	}
}
